<?php
/**
 * Created by nineton.
 * Description:黑名单服务类
 * User: sunnier
 * Email: xiaoyao_xiao@126.com
 * Date: 2019-12-01 12:46
 */
namespace app\common\service;

class BlackListService extends BaseService
{
    protected $blackListModel;

    public function __construct()
    {
        parent::__construct();
        $this->blackListModel=model('common/BlackList');
    }

    /**
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * Description:详情
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-18 15:11
     */
    public function detail($where){
        $data=$this->blackListModel->where($where)
            ->where('status',1)->find();
        return [
            'status'=>1,
            'msg'=>'获取成功',
            'data'=>$data
        ];
    }
    /**
     * @param array $where
     * @param int $page
     * @param string $order
     * @param int $listRow
     * @return array
     * Description:获取列表
     * User: sunnier
     * Email: xiaoyao_xiao@126.com
     * Date: time
     */
    public function getList($where=[],$page=1,$listRow=10,$order='sort_num DESC,id DESC'){
        $count=$this->blackListModel->where($where)->where('status',1)->count();
        if($count>0){
            $list=$this->blackListModel->field('id,type,title,description,name,url')
                ->where($where)->where('status',1)
                ->order($order)->page($page,$listRow)->select();
            return [
                'status'=>1,
                'msg'=>'获取成功！',
                'data'=>$list
            ];
        }else{
            $list=[];
            return [
                'status'=>1,
                'msg'=>'没数据了',
                'data'=>$list
            ];
        }
    }

    /**
     * @param $data
     * @return array
     * Description:添加企业
     * Author:sunnier
     * Email:xiaoyao_xiao@126.com
     * 2020-11-27 12:26
     */
    public function addCompany($data){
        try {
            if (empty($data['province_id']) || empty($data['title']) || empty($data['comment'])) {
                return [
                    'status' => -10000,
                    'msg' => '参数不完整',
                    'data' => []
                ];
            }
            if (!empty($data['city_name'])) {
                $name = $data['city_name'] . '的黑名单企业';
            } else {
                $name = '';
            }
            $nowTime = time();
            $blackData = [
                'type' => 1,
                'province_id' => $data['province_id'],
                'city_id' => $data['city_id'],
                'title' => filterEmoji($data['title']),
                'description' => $data['province_name'] . '的黑名单企业',
                'name' => $name,
                'status' => 0,
                'create_time' => $nowTime,
                'update_time' => $nowTime
            ];
            $blackId = $this->blackListModel->insertGetId($blackData);
            $commentData = [
                'black_id' => $blackId,
                'content' => filterEmoji(strval($data['comment'])),
                'create_time' => $nowTime,
                'update_time' => $nowTime
            ];
            $commentModel = model('common/Comment');
            $commentModel->insert($commentData);
            return [
                'status' => 1,
                'msg' => '提交成功！',
                'data' => []
            ];
        }catch (\Exception $e){
            return [
                'status' => -10000,
                'msg' => '提交失败！',
                'data' => []
            ];
        }
    }
}